//
//  NSString+YMJMD5.m
//  MD5
//
//  Created by 殷殷明静 on 16/8/16.
//  Copyright © 2016年 殷明静. All rights reserved.
//

#import "NSString+YMJMD5.h"
#import <CommonCrypto/CommonCrypto.h>
@implementation NSString (YMJMD5)
- (NSString *)stringWithMD5 {
    // 1.将字符串转换成C语言字符串
    const char *cString = [self UTF8String];
    // 2.将C的字符串转换 MD5
    CC_LONG length = (CC_LONG)strlen(cString);
    unsigned char bytes[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cString, length, bytes);
    // 3.将C的字符串转换成OC的字符串
    NSMutableString *string = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i ++) {
        [string appendFormat:@"%02x",bytes[i]];
    }
    return string;
}
- (NSString *)stringWith32MD5Lower {
    
    return [[self stringWithMD5] lowercaseString];
}
- (NSString *)stringWith32MD5Upper {
    return [[self stringWithMD5] uppercaseString];
}
- (NSString *)stringWith64MD5Lower {
    return [[[self stringWith32MD5Lower] substringToIndex:8] substringFromIndex:8];
}
- (NSString *)stringWith64MD5Upper {
    return [[[self stringWith32MD5Upper] substringToIndex:8] substringFromIndex:8];;
}
@end
